import router from './router'
import store from '@/store/index'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
const pageArr = ['/404', '/login']
router.beforeEach(async(to, from, next) => {
  Nprogress.start()
  if (store.getters.token) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (pageArr.includes(to.path.toLowerCase())) {
      next()
    } else {
      // console.log(to)
      //   next('/login?redirect=' + to.currentRoute.fullPath)
      //  建议都加上encodeURIComponent ie不支持中文字符
      next(`/login?redirect= ${encodeURIComponent(to.fullPath)}`)
    }
  }
  Nprogress.done()
})
router.afterEach((to, from) => {
  Nprogress.done()
})
